package com.example.homework.ui.main

import android.graphics.LightingColorFilter
import android.os.Bundle
import android.provider.MediaStore.Audio.Radio
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.TextView
import androidx.activity.OnBackPressedCallback
import androidx.navigation.fragment.findNavController
import com.example.homework.R
import com.example.homework.databinding.FragmentMainBinding
import com.example.homework.quiz.Quiz
import com.example.homework.quiz.QuizStorage
import com.google.android.material.resources.TextAppearance

class MainFragment : Fragment() {

    private var _binding: FragmentMainBinding? = null
    private val binding: FragmentMainBinding
        get() = _binding!!

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val onBackPressedCallback = object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                findNavController().navigate(R.id.action_mainFragment_to_welcomeFragment)
            }
        }
        requireActivity().onBackPressedDispatcher.addCallback(onBackPressedCallback)
    }

    private val quiz: Quiz = QuizStorage.getQuiz(QuizStorage.Locale.Ru);
    private var answers: MutableList<Int> =
        (0 until quiz.questions.size).toMutableList()

    private fun changeAnswer(i: Int, value: Int) {
        answers[i] = value
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding = FragmentMainBinding.inflate(inflater, container, false)
        for (i in 0..answers.size - 1) {
            answers[i] = -1
        }
        var questionIterator: Int = 0
        var answerIterator: Int = 0
        val resultButton: Button = Button(context)
        resultButton.isEnabled = false
        for (i in quiz.questions) {
            val questionText: TextView = TextView(context)
            questionText.setTextAppearance(R.style.Theme_Homework_Question)
            questionText.text = "${questionIterator + 1}) ${i.question}"
            val answersRadioGroup: RadioGroup = RadioGroup(context)
            answersRadioGroup.tag = questionIterator
            answerIterator = 0
            for (j in i.answers) {
                val answerRadioButton: RadioButton = RadioButton(context)
                answerRadioButton.text = j
                answerRadioButton.tag = answerIterator
                answerRadioButton.setOnClickListener {
                    val parent: RadioGroup = it.parent as RadioGroup
                    changeAnswer(parent.tag as Int, it.tag as Int)
                    if (!(-1 in answers))
                        resultButton.isEnabled = true
                }
                answersRadioGroup.addView(answerRadioButton)
                answerIterator++
            }
            questionIterator++
            binding.questionsLinearLayout.addView(questionText)
            binding.questionsLinearLayout.addView(answersRadioGroup)
        }
        resultButton.text = "Отправить"
        resultButton.setOnClickListener {
            val res = QuizStorage.answer(quiz, answers)
            val bundle: Bundle = Bundle().apply {
                putString("resultText", res)
            }
            findNavController().navigate(R.id.action_mainFragment_to_resultFragment, bundle)
        }
        binding.questionsLinearLayout.addView(resultButton)
        return binding.root
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    companion object {
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            MainFragment().apply { }
    }
}